package service

import (
	"seat-service/initialization"
	"seat-service/model"
	"strconv"
	"time"
)

type StudentService struct {
}

func (*StudentService) Create(student *model.Student) error {
	return initialization.DB.Create(&student).Error
}

func (*StudentService) GetByOpenid(openid string) (student model.Student) {
	initialization.DB.Where("openid = ?", openid).First(&student)
	return
}

func (*StudentService) LoginByNameAndStudentNumber(student *model.Student) (err error) {
	err = initialization.DB.Where("name = ? AND student_number = ?", student.Name, student.StudentNumber).First(&student).Error
	return
}

func (*StudentService) ViolateStudent(studentId string, lockStartTime time.Time, lockEndTime time.Time) (studentInformation model.Student, err error) {
	violateStudent := model.Student{
		StudentStatus:          1,
		ViolationLockTimeStart: &lockStartTime,
		ViolationLockTimeEnd:   &lockEndTime,
	}
	id, _ := strconv.Atoi(studentId)
	err = initialization.DB.Where("id = ?", id).Updates(&violateStudent).Error
	if err != nil {
		return
	}
	err = initialization.DB.Where("id = ?", id).Find(&studentInformation).Error
	return
}
